package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.domain.TProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.CategoryTreeMapper;
import com.ruoyi.business.domain.CategoryTree;
import com.ruoyi.business.service.ICategoryTreeService;

/**
 * 美团树形结构Service业务层处理
 * 
 * @author 超
 * @date 2025-03-13
 */
@Service
public class CategoryTreeServiceImpl implements ICategoryTreeService 
{
    @Autowired
    private CategoryTreeMapper categoryTreeMapper;

    /**
     * 查询美团树形结构
     * 
     * @param id 美团树形结构主键
     * @return 美团树形结构
     */
    @Override
    public CategoryTree selectCategoryTreeById(Long id)
    {
        return categoryTreeMapper.selectCategoryTreeById(id);
    }

    /**
     * 查询美团树形结构列表
     * 
     * @param categoryTree 美团树形结构
     * @return 美团树形结构
     */
    @Override
    public List<CategoryTree> selectCategoryTreeList(CategoryTree categoryTree)
    {
        return categoryTreeMapper.selectCategoryTreeList(categoryTree);
    }

    /**
     * 新增美团树形结构
     * 
     * @param categoryTree 美团树形结构
     * @return 结果
     */
    @Override
    public int insertCategoryTree(CategoryTree categoryTree)
    {
        return categoryTreeMapper.insertCategoryTree(categoryTree);
    }

    /**
     * 修改美团树形结构
     * 
     * @param categoryTree 美团树形结构
     * @return 结果
     */
    @Override
    public int updateCategoryTree(CategoryTree categoryTree)
    {
        return categoryTreeMapper.updateCategoryTree(categoryTree);
    }

    /**
     * 批量删除美团树形结构
     * 
     * @param ids 需要删除的美团树形结构主键
     * @return 结果
     */
    @Override
    public int deleteCategoryTreeByIds(Long[] ids)
    {
        return categoryTreeMapper.deleteCategoryTreeByIds(ids);
    }

    /**
     * 删除美团树形结构信息
     * 
     * @param id 美团树形结构主键
     * @return 结果
     */
    @Override
    public int deleteCategoryTreeById(Long id)
    {
        return categoryTreeMapper.deleteCategoryTreeById(id);
    }

    /**
     * 美图树形
     * @param tProductCategory
     * @return
     */
    @Override
    public List<CategoryTree> CategoryTree(CategoryTree tProductCategory) {
        List<CategoryTree> categoryTrees = categoryTreeMapper.selectCategoryTreeList(tProductCategory);
        return buildCategoryTreePlus(categoryTrees,0L);
    }

    private List<CategoryTree> buildCategoryTreePlus(List<CategoryTree> categoryTrees, long parentId) {
        // 存储树形结构
        List<CategoryTree> tree = new ArrayList<>();

        // 遍历所有分类
        for (CategoryTree category : categoryTrees) {
            // 如果当前分类的parentId与给定的parentId相等，则是该父分类的子分类
            if (category.getParentId().equals(parentId)) {
                // 获取该父分类下的子分类
                List<CategoryTree> children = buildCategoryTreePlus(categoryTrees, category.getId());
                category.setChildren(children); // 设置子分类
                tree.add(category); // 添加到树形结构中
            }
        }
        return tree;
    }
}
